package com.ikun.controller;


import ch.qos.logback.core.sift.AppenderFactoryUsingSiftModel;
import com.ikun.pojo.Business;
import com.ikun.pojo.Order;
import com.ikun.pojo.Result;
import com.ikun.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;


    @GetMapping("/listOrder")
    public Result getAllOrders(Integer userId) {
        return Result.success(orderService.getAllOrder(userId)) ;
    }

    @GetMapping("/getOrderById/{id}")
    public Result getOrderById(@PathVariable("id") Integer id){
        return Result.success(orderService.getOrderById(id));
    }

    @GetMapping("/getBusinessByUserIdAndTypeId")
    public List<Business> getBusinessByUserIdAndTypeId(Integer userId,Integer TypeId){
        return orderService.getBusinessByUserIdAndTypeId(userId,TypeId);
    }
    //添加订单,添加的字段为userId,businessId,price,shippingAddress,
    @GetMapping("/addOrder")
    public Result addOrder(Integer userId,Integer businessId,Float price,String shippingAddress){
        return Result.success(orderService.addOrder(userId,businessId,price,shippingAddress));
    }
    //根据用户id查询订单表中的对应的商家信息
    @GetMapping("/getBusinessByUserId/{userId}")
    public Result getBusinessByUserId(@PathVariable("userId") Integer userId){
        return Result.success(orderService.getOrderBusinessInfo(userId));
    }
    //根据orderId修改order_status
    @GetMapping("/updateOrderStatus")
    public Result updateOrderStatusByOrderId(Integer orderId,Integer orderStatus){
        int result=orderService.updateOrderStatus(orderId,orderStatus);
        if(result>0){
            return Result.success(null);
        } else{
            return Result.error("修改状态失败");
        }
    }
    //根据userId获取买过的id
    @GetMapping("/getBoughtIdByUserId")
    public Result getBusinessIdByUserId(Integer userId){
        return Result.success(orderService.getBuyBusiness(userId));
    }
}
